<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>多种居中</title>

  <style>
    /* 上下左右居中1  position*/
    .center1 {
      width: 200px;
      height: 200px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: aliceblue;
    }

    /* 上下左右居中1  flex*/
    .center2 {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    /* 上下左右居中1  table*/
    .center3 {
      display: table;
      width: 500px;
      height: 500px;
    }

    .cell {
      display: table-cell;
      /* 上下居中vertical-align */
      vertical-align: middle;
      /* 左右居中 text-align */
      text-align: center;
      width: 200px;
      height: 200px;
      background-color: aliceblue;
    }
  </style>
</head>

<body>
  <div style="text-align:center">
    <span>如果display是inline或者inline-block的话，text-align:center可以左右居中</span>
  </div>

  <div style="margin: 0 auto; width: 200px;">
    元素是block;marin: 0 auto让其自身左右居中
  </div>

  <div style="width:500px;height:500px;position: relative; background: #000;">
    <div class="center1"></div>
  </div>

  <div class="center2" style="width:500px;height:500px; background: #000;margin-top: 200px;">
    <div style="width:200px;height:200px; background-color: aliceblue;"></div>
  </div>


  <div class="center3" style="width:500px;height:500px; margin-top: 200px;">
    <div class="cell">cell的内容是居中的</div>
  </div>




</body>

</html>